package com.nmgsr.ptmsapp.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.nmgsr.ptmsapp.interfaces.WebAppInterface;

public class EchartViewMap extends WebView {
    public EchartViewMap(Context context) {
        super(context);
        init();
    }

    public EchartViewMap(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        WebSettings settings = getSettings();
        settings.setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            settings.setDomStorageEnabled(true);
        }

        addJavascriptInterface(new WebAppInterface(getContext()), "android");

        setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                Log.d("EchartViewMap", "Page finished loading: " + url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.e("EchartViewMap", "Error loading page: " + description);
            }
        });

        loadUrl("file:///android_asset/inner_mongolia_map.html");
    }

    public void refreshEchartsWithOption(String optionJson) {
        evaluateJavascript("javascript:chart.setOption(" + optionJson + ");", null);
    }
}
